﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common;
using WFNetLib;
using WFNetLib.StringFunc;
using System.Data;
using DAL;

namespace 服务器端.webAPI
{
    public partial class API_Reg : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string id;
            RegResult ret = new RegResult();
            try
            {
                id = Request.QueryString["ID"];
                //信息|用户id|实验id，实验名，实验室;
                if (string.IsNullOrEmpty(id))//没有这两个变量
                {
                    ret.endcodeID = "";
                    ret.message = "no id";
                }
                else
                {
                    byte[] bid = StringsFunction.strToHexByte(id, "");
                    lcyHashCal lcy = new lcyHashCal();
                    id = StringsFunction.byteToHexStr(lcy.HashCal(bid, 5), "");
                    TEInfoData te=TEInfoDataDBOption.GetTEInfo(id);
                    if (te!=null)
                    {
                        ret.endcodeID = te.ID;
                        ret.zhuCeID = te.zhuceID;
                        ret.message = "ok";
                        TEPulseData te1 = TEPulseDataDBOption.CheckTE(id);
                        TEPulseDataDBOption.UpdateLastPulse(id);
                        if (te1 != null)
                        {
                            DateTime dt = te1.time;
                            te1.time = DateTime.Now;
                            TEPulseDataDBOption.Update(te1, dt);                            
                        }
                        else
                        {
                            te1 = new TEPulseData();
                            te1.TEID = id;
                            te1.time = DateTime.Now;
                            TEPulseDataDBOption.Insert(te1);
                        }
                    }
                    else
                    {
                        ret.endcodeID = "";
                        ret.message = "Unreg";
                    }
                }
            }
            catch (System.Exception ex)
            {
                ret.endcodeID = "";
                ret.message = "未知错误" + ex.Message;                
            }
            ret.DateTime = DateTime.Now.ToString();
            Response.Write(WFJSON.JsonSerializerBySingleData<RegResult>(ret));
        }
    }
}